package work.display
{
	/**
	 * 
	 * @author 张峰
	 * @QQ:37520626
	 * @Email:7677@163.com
	 * @Date:2012-10-18 下午3:41:21
	 * 
	 */
	import flash.display.DisplayObject;
	import flash.events.Event;
	
	import work.display.SuperMovieClipEvent;
	
	import zf.display.FlashMovieClipComponent;
	import zf.display.tools.DisplayLoader;
	import zf.display.tools.DisplayLoaderArray;
	
	/**
	 * 加载多个完成事件 
	 * @eventType work.display.SuperMovieClipEvent.LOAD_LIST_INIT
	 */
	[Bindable]
	[Event(name="load_list_init", type="work.display.SuperMovieClipEvent")]
	
	/**
	 * 加载单个完成事件 
	 * @eventType work.display.SuperMovieClipEvent.LOAD_INIT
	 */
	[Bindable]
	[Event(name="load_init", type="work.display.SuperMovieClipEvent")]
	
	/**
	 * 倒播放 
	 * @eventType work.display.SuperMovieClipEvent.REWIND
	 */
	[Bindable]
	[Event(name="rewind", type="work.display.SuperMovieClipEvent")]
	
	public class SuperMovieClip extends FlashMovieClipComponent
	{
		
		private var _isLoaded:Boolean=false;
		private var _isListLoaded:Boolean=false;
		
		private var rewindNum:int;
		private var _rewindRun:Boolean=false;
		private var _rewindLoop:Boolean=false;
		public function SuperMovieClip()
		{
			super();
		}
		
		/**
		 * 是否可以循环滚动，如果可以，滚到比最后一帧大的时候就会从第一帧重新计算 
		 * @return 
		 * 
		 */
		public function get rewindLoop():Boolean
		{
			return _rewindLoop;
		}

		public function set rewindLoop(value:Boolean):void
		{
			_rewindLoop = value;
		}

		/**
		 * 是否在执行倒播放 
		 * @return 
		 * 
		 */
		public function get rewindRun():Boolean
		{
			return _rewindRun;
		}

		/**
		 * 是否正在进行多个加载 
		 * @return 
		 * 
		 */
		public function get isListLoaded():Boolean
		{
			return _isListLoaded;
		}

		/**
		 * 是否正在进行单个加载 
		 * @return 
		 * 
		 */
		public function get isLoaded():Boolean
		{
			return _isLoaded;
		}

		/**
		 * 加载一个可以显示的对象 
		 * @param s 地址
		 * 
		 */
		public function loadDisplay(s:String):void
		{
			_isLoaded=true;
			var loader:DisplayLoader=new DisplayLoader();
			loader.addEventListener(Event.INIT,onLoadInitHandler);
			loader.load(s);
		}
		private function onLoadInitHandler(e:Event):void
		{
			_isLoaded=false;
			var loader:DisplayLoader=DisplayLoader(e.currentTarget);
			var content:DisplayObject=loader.content;
			this.dispatchEvent(new SuperMovieClipEvent(SuperMovieClipEvent.LOAD_INIT,content));
			loader.removeEventListener(Event.INIT,onLoadInitHandler);
			loader.dispose();
		}
		
		/**
		 * 加载多个可以显示的对象 
		 * @param arr
		 * 
		 */
		public function loadDisplayList(arr:Vector.<String>):void
		{
			_isListLoaded=true;
			var loader:DisplayLoaderArray=new DisplayLoaderArray(arr);
			loader.addEventListener(Event.COMPLETE,onLoadListCompleteHandler);
			loader.start();
		}
		
		private function onLoadListCompleteHandler(e:Event):void
		{
			_isListLoaded=false;
			var loader:DisplayLoaderArray=DisplayLoaderArray(e.currentTarget);
			var arr:Array=loader.contents;
			this.dispatchEvent(new SuperMovieClipEvent(SuperMovieClipEvent.LOAD_LIST_INIT,arr));
			loader.removeEventListener(Event.COMPLETE,onLoadListCompleteHandler);
			loader.dispose();
		}
		
		/**
		 * 倒播放,此行为会忽略帧上stop()动作,但是不会忽略gotoAndStop()和gotoAndPlay()
		 * @param o 如果是String 则以当前帧为开始帧，移动o个帧，如果是uint,则移动到1帧
		 * 
		 */
		public function rewindPlay(o:Object=null):void
		{
			if(_rewindRun)
			{
				this.removeEventListener(Event.ENTER_FRAME,onRinwdPlayPrivateClassEnterFrameHandler);
			}
			_rewindRun=true;
			try
			{
				if(o==null)
				{
					rewindNum=1;
				}else
				{
					if(o is String)
					{
						rewindNum=this.currentFrame+int(o);
					}else
					{
						rewindNum=int(o);
					}
					if(rewindLoop)
					{
						var num:int=rewindNum-1;
						//var total:uint=this.totalFrames;
						
						//num=(num+total)%total;
						rewindNum=num+1;
					}else
					{
						rewindNum=rewindNum>1?rewindNum:1;
						rewindNum=rewindNum>this.totalFrames?this.totalFrames:rewindNum;
					}
				}
			}catch(e:Error)
			{
				rewindNum=1;
			}
			this.addEventListener(Event.ENTER_FRAME,onRinwdPlayPrivateClassEnterFrameHandler);
		}
		
		private function onRinwdPlayPrivateClassEnterFrameHandler(e:Event):void
		{
			var cf:int=this.currentFrame-1;
			var target:int=rewindNum-1;
			var total:uint=this.totalFrames;
			if(cf==target%total)
			{
				this.removeEventListener(Event.ENTER_FRAME,onRinwdPlayPrivateClassEnterFrameHandler);
				this.dispatchEvent(new SuperMovieClipEvent(SuperMovieClipEvent.REWIND));
				_rewindRun=false;
				return;
			}
			if(cf>target)
			{
				this.gotoAndStop((cf-1+total)%total+1);
			}else
			{
				this.gotoAndStop((cf+1)%total+1);
			}
		}
	}
}